<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DriverLable extends Model
{
    //
    protected $table = 'v_driver_lable';

    /*保存信息
     * @param $ids=23,24,25
     * */

    public function store($ids, $lable_name, $lable_id, $owner_id, $owner_uuid)
    {
        $ids = explode(',', $ids);
        foreach ($ids as $item) {
            $model = new DriverLable();
            // $model->created_at = date('Y-m-d H:i:s', time());
            // $model->lable_name = $lable_name;
            $data['lable_name'] = $lable_name;
            $data['label_id'] = $lable_id;
            $data['owner_id'] = $owner_id;
            $data['driver_mail_id'] = $item;
            $data['owner_uuid'] = $owner_uuid;
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $model::insert($data);
        }
        return true;
    }

    /***sql
     * 获取某个标签下的所有司机手机号
     * $label_id 标签id号码
     */
    public function getListByLabelId($label_id)
    {
        $phoneArr = DriverLable::query()->where('label_id', $label_id)->leftJoin('v_drivers_mail_list as l', 'l.id', '=', 'v_driver_lable.driver_mail_id')->get(['phone'])->toArray();
        $phonestr = '';
        foreach ($phoneArr as $phone) {
            $phonestr .= $phone['phone'] . ',';
        }
        return rtrim($phonestr, ',');
    }

    /***sql
     * 获取某个标签下的所有司机列表
     * $label_id 标签id号码
     */
    public function getListsByLabelId($label_id)
    {
        $phoneArr = DriverLable::query()->where('label_id', $label_id)->leftJoin('v_drivers_mail_list as l', 'l.id', '=', 'v_driver_lable.driver_mail_id')->get(['phone','label_id'])->toArray();
        return $phoneArr;
    }

}
